package reno.alerts.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reno.alerts.dao.AlertsDao;
import reno.alerts.entity.AlertsEO;
import reno.alerts.entity.StarsEO;
import reno.alerts.entity.TypesEO;
import reno.alerts.mo.AlertsMO;
import reno.alerts.mo.ResponseMO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class AlertsService {

    @Autowired
    AlertsDao alertsDao;

    private String explainType(String type, Boolean nightmare, Boolean archwingRequired) {
        String original = type;
        String result = "TYPENIGHTARCHW";
        List<TypesEO> typesEOList = alertsDao.queryType(type);
        for (TypesEO eachEO : typesEOList) {
            original = eachEO.getZh();
            break;
        }
        result = result.replace("TYPE", original);
        result = result.replace("NIGHT", nightmare ? "(噩梦)" : "");
        result = result.replace("ARCHW", nightmare ? "(空战)" : "");
        return result + "\n";
    }

    private String explainNode(String node) {
        String original = node;
        node = node.substring(node.indexOf("(") + 1, node.lastIndexOf(")"));
        List<StarsEO> starsEOList = alertsDao.queryStars(node);
        for (StarsEO eachEO : starsEOList) {
            original = original.replace(node, eachEO.getZh());
            break;
        }
        return original + "\n";
    }

    private String explainReward(String rewardString) {
        String[] splitReward = rewardString.split("\\+");
        List<String> stringList = Arrays.asList(splitReward);
        String rewardOne;
        String rewardTwo;
        if (stringList.size()>1){
            rewardOne = stringList.get(0).replaceAll("\\d+", "");
            rewardTwo = stringList.get(1);
            List<AlertsEO> alertsEOList = alertsDao.queryAlerts(rewardOne);
            for (AlertsEO eachEO:alertsEOList) {
                rewardOne = stringList.get(0).replace(rewardOne, eachEO.getZh());
                break;
            }
            return "物品奖励:" + rewardOne + "\n" + "星币奖励:" + rewardTwo;
        } else {
            rewardOne = stringList.get(0);
            return "星币奖励:" + rewardOne;
        }

    }

    public List<ResponseMO> parseList(String alertsString) {
        List<AlertsMO> alertsmOList = JSONObject.parseArray(alertsString, AlertsMO.class);
        List<ResponseMO> responseMOList = new ArrayList<>();
        for (AlertsMO eachMO : alertsmOList) {
            ResponseMO responseMO = new ResponseMO(eachMO);
            responseMOList.add(responseMO);
        }
        return responseMOList;
    }

    public String explain(List<ResponseMO> responseMOList) {
        StringBuilder stringBuilder = new StringBuilder("Ordis监测到当前警报如下");
        for (ResponseMO eachMO : responseMOList) {
            stringBuilder.append("\n------------\n");
            stringBuilder.append(explainType(eachMO.getType(), eachMO.isNightmare(), eachMO.isArchwingRequired()));
            stringBuilder.append(explainNode(eachMO.getNode()));
            stringBuilder.append(explainReward(eachMO.getAsString()));
        }
        return stringBuilder.toString();
    }
}
